package com.zml.control.okhttp;

import com.zml.control.ControlMainAbility;
import com.zml.control.database.PreferenceDataBase;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import okhttp3.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class HuaweiCloudApi {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0, "HuaweiCloudApi");
    private static OkHttpClient okHttpClient = new OkHttpClient();
    private static String IAMUserToken = "";
    private final Map<String, Object> dataMap = new HashMap<>();

    public static void getIAMUserToken(Context context) throws IOException {                        //1.创建OkHttpClient对象
        MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
        String requestBody = "{\n" +
                "\t\"auth\":{\n" +
                "\t\t\"identity\":{\n" +
                "\t\t\t\"methods\":[\"password\"],\n" +
                "\t\t\t\"password\":{\n" +
                "\t\t\t\t\"user\":{\n" +
                "\t\t\t\t\t\"name\":\"gitee\",\n" +
                "\t\t\t\t\t\"password\":\"Gitee123.123\",\n" +
                "\t\t\t\t\t\"domain\":{\n" +
                "\t\t\t\t\t\t\"name\":\"Bright_ML\"\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"scope\":{\n" +
                "\t\t\t\"project\":{\n" +
                "\t\t\t\t\"name\": \"cn-north-4\"\n" +
                "\t\t\t}\n" +
                "\t\t}\n" +
                "\t}\n" +
                "}";

        Request request = new Request.Builder()                                     //2.创建Request对象，设置一个url地址,设置请求方式。
                .url("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens")
                .addHeader("Content-Type", "application/json")
                .post(RequestBody.create(mediaType, requestBody))
                .build();

//        try (Response response = okHttpClient.newCall(request).execute()) {
//            return response.body().string();
//        }
        okHttpClient.newCall(request).enqueue(new Callback() {                       //3.创建一个call对象,参数就是Request请求对象
            @Override
            public void onFailure(Call call, IOException e) {
                HiLog.info(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                HiLog.info(TAG, response.protocol() + " " +response.code() + " " + response.message());
                Headers headers = response.headers();
                for (int i = 0; i < headers.size(); i++) {
                    HiLog.info(TAG, headers.name(i) + ":" + headers.value(i));
                    if(Objects.equals(headers.name(i), "X-Subject-Token")){
                        // 保存到轻量级数据存储
                        PreferenceDataBase.SaveIAMUserToken(context,headers.value(i));
                        HiLog.info(TAG, "ZML" + headers.value(i));
                    }
                }
                HiLog.info(TAG, "onResponse: " + response.body().string());
            }
        });
    };

    public static String showDeviceShadow(Context context) throws IOException {
        Request request = new Request.Builder()
                .url("https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/0c2785b9a98010d52f9dc00c70d11445/devices/62687d6cb9f9fa4a4f9e3f17_OHOS_MPPT002/shadow")
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Auth-Token", PreferenceDataBase.GetIAMUserToken(context))
                .build();
        //3.创建一个call对象,参数就是Request请求对象
        try (Response response = okHttpClient.newCall(request).execute()) {
            return response.body().string();
        }
    }
}
